package com.hp.btc.controller;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.ModelAttribute;

import com.hp.btc.bean.BeanAudio;
import com.hp.btc.bean.BeanBalanceTransfer;
import com.hp.btc.bean.BeanCampaign;
import com.hp.btc.bean.BeanVDN;
import com.hp.btc.service.DescriptionVDNService;
import com.hp.btc.service.ListVDNService;
import com.hp.btc.util.JsonCustomResources;

public class BaseController {
	
	BeanBalanceTransfer balanceTranfer = null;
	
	private static final Logger logger = LoggerFactory.getLogger(BaseController.class);
	
	@Autowired
	ListVDNService vdnService;
	
	@Autowired
	DescriptionVDNService desService;
	
	@Autowired
	JsonCustomResources util;
	
	@Value("${timeout}")
	protected String timeout;
	@Value("${btnSave}")
	protected String btnSave;
	@Value("${btnUpdate}")
	protected String btnUpdate;
	@Value("${btnExport}")
	protected String btnExport;
	@Value("${serviceAudit}")
	protected String serviceAudit;
	
	protected String path = "";
	
	Integer currentHour;
	
	Integer currentDay;
	
	@PostConstruct
	protected void loadFiles() {
		logger.info("Initial load files...");
		reloadFiles();
		
		desService.putListCampaing(balanceTranfer.getListCampaigns());
		desService.putCurrentDescriptionVDNs(balanceTranfer.getService_description());
		
		Calendar currentDate = new GregorianCalendar();
		
		currentDay = currentDate.get(Calendar.DAY_OF_WEEK);
		currentHour = currentDate.get(Calendar.HOUR_OF_DAY);
		
	}
	
	protected void reloadFiles(){
		logger.info("Initial reload files...");
		path = path.equals("")?util.getRootFileJson():path;
		logger.debug("path {}",path);
		Object obj = util.readFiletoPath(BeanBalanceTransfer.class, path);
		balanceTranfer = (BeanBalanceTransfer)obj;
		
		desService.putDescripctionVDNs(balanceTranfer.getService_description());
	}
	
	@ModelAttribute("audiosMessages")
	public List<BeanAudio> messageError(){
		return balanceTranfer.getAudios_message_error();
	}
	
	@ModelAttribute("vdns")
	public List<BeanVDN> listVdns(){
		return balanceTranfer.getListVDN();
	}
	
	@ModelAttribute("audioServiceHours")
	public List<BeanAudio> audioServiceHour(){
		return balanceTranfer.getAudio_service_hours();
	}
	
	@ModelAttribute("audioOutServiceHours")
	public List<BeanAudio> audioOutServiceHour(){
		return balanceTranfer.getAudio_out_service_hours();
	}
	
	@ModelAttribute("campaigns")
	public List<BeanCampaign> listCampaigns(){
		return balanceTranfer.getListCampaigns();
	}
	
	@ModelAttribute("campaignsDiscrete")
	public List<BeanCampaign> campaignsDiscrete(){
		List<BeanCampaign> campaigns = balanceTranfer.getListCampaigns();
		Map<String, BeanCampaign> mapCampaign = new HashMap<String, BeanCampaign>();
		for (BeanCampaign campaign : campaigns) {
			if(!mapCampaign.containsKey(campaign.getName()))
				mapCampaign.put(campaign.getName(), campaign);
		}
		campaigns = new ArrayList<BeanCampaign>();
		campaigns.addAll(mapCampaign.values());
		return campaigns;
	}
}
